package com.ruoyi.mespro.controller;

import java.util.List;

import com.ruoyi.mespro.domain.AlldataSizing;
import com.ruoyi.mespro.domain.ShaftDefectDetail;
import com.ruoyi.mespro.domain.ShaftSize;
import com.ruoyi.mespro.service.IAlldataSizingService;
import com.ruoyi.mespro.service.IShaftDefectDetailService;
import com.ruoyi.mespro.service.IShaftSizeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mespro.domain.ShaftDefect;
import com.ruoyi.mespro.service.IShaftDefectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 疵点管理Controller
 *
 * @author JunXiang
 * @date 2022-08-02
 */
@RestController
@RequestMapping("/mesana/defect")
public class ShaftDefectController extends BaseController
{
    @Autowired
    private IShaftDefectService shaftDefectService;
    @Autowired
    private IShaftDefectDetailService shaftDefectDetailService;
    @Autowired
    private IAlldataSizingService alldataSizingService;
    @Autowired
    private IShaftSizeService shaftSizeService;

    /**
     * 查询疵点管理列表
     */
    /*@PreAuthorize("@ss.hasPermi('mesana:defect:list')")*/
    @GetMapping("/list")
    public TableDataInfo list(ShaftDefect shaftDefect)
    {
        startPage();
        List<ShaftDefect> list = shaftDefectService.selectShaftDefectList(shaftDefect);
        return getDataTable(list);
    }

    /**
     * 导出疵点管理列表
     */
   /* @PreAuthorize("@ss.hasPermi('mesana:defect:export')")*/
    @Log(title = "疵点管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ShaftDefect shaftDefect)
    {
        List<ShaftDefect> list = shaftDefectService.selectShaftDefectList(shaftDefect);
        ExcelUtil<ShaftDefect> util = new ExcelUtil<ShaftDefect>(ShaftDefect.class);
        return util.exportExcel(list, "疵点管理数据");
    }

    /**
     * 获取疵点管理详细信息
     */
   /* @PreAuthorize("@ss.hasPermi('mesana:defect:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(shaftDefectService.selectShaftDefectById(id));
    }

    /**
     * 新增疵点管理
     */
    /*@PreAuthorize("@ss.hasPermi('mesana:defect:add')")*/
    @Log(title = "疵点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShaftDefectDetail shaftDefectDetail)
    {
        String maccode = shaftDefectDetail.getMaccode();
        String defectname = shaftDefectDetail.getDefectname();
        String mid = maccode.split("-")[0];
        String station = maccode.split("-")[1];
        AlldataSizing alldataSizing = new AlldataSizing();
        alldataSizing.setMid(mid);
        alldataSizing.setStation(station);
        List<AlldataSizing> list = alldataSizingService.selectAlldataSizingList(alldataSizing);
        AlldataSizing a = list.get(0);
        shaftDefectDetail.setShaftcode(a.getZzno());
        shaftDefectDetail.setLocation(a.getBeamlength());
        ShaftSize shaftSize = shaftSizeService.selectShaftSizeByShaftCode(a.getZzno());
        shaftDefectDetail.setShaftcode(shaftSize.getShaftcode());
        shaftDefectDetail.setConstruction(shaftSize.getConstruction());
        shaftDefectDetail.setJarno(shaftSize.getJarNo());
        shaftDefectDetail.setShaftdate(shaftSize.getShiftdate());
        shaftDefectDetail.setShafttype(shaftSize.getShifttype());
        shaftDefectDetailService.insertShaftDefectDetail(shaftDefectDetail);
        ShaftDefect shaftDefect = shaftDefectService.selectShaftDefectByShaftCode(shaftDefectDetail.getShaftcode());
        if (defectname.equals("jiangban")) {
            shaftDefect.setJiangban(shaftDefect.getJiangban() + shaftDefectDetail.getAmount());
        }
        if (defectname.equals("jiangyou")) {
            shaftDefect.setJiangyou(shaftDefect.getJiangyou() + shaftDefectDetail.getAmount());
        }
        if (defectname.equals("bingsha")) {
            shaftDefect.setBingsha(shaftDefect.getBingsha() + shaftDefectDetail.getAmount());
        }
        if (defectname.equals("jiaotou")) {
            shaftDefect.setJiaotou(shaftDefect.getJiaotou() + shaftDefectDetail.getAmount());
        }
        if (defectname.equals("jiangcaokd")) {
            shaftDefect.setJiangcaokd(shaftDefect.getJiangcaokd() + shaftDefectDetail.getAmount());
        }
        if (defectname.equals("jingzhoukd")) {
            shaftDefect.setJingzhoukd(shaftDefect.getJingzhoukd() +shaftDefectDetail.getAmount());
        }
        if (defectname.equals("chetouzk")) {
            shaftDefect.setChetouzk(shaftDefect.getChetouzk() + shaftDefectDetail.getAmount());
        }
        if (defectname.equals("huaibian")) {
            shaftDefect.setHuaibian(shaftDefect.getHuaibian() + shaftDefectDetail.getAmount());
        }
        return toAjax(shaftDefectService.updateShaftDefect(shaftDefect));
    }

    /**
     * 修改疵点管理
     */
   /* @PreAuthorize("@ss.hasPermi('mesana:defect:edit')")*/
    @Log(title = "疵点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShaftDefect shaftDefect)
    {
        return toAjax(shaftDefectService.updateShaftDefect(shaftDefect));
    }

    /**
     * 删除疵点管理
     */
   /* @PreAuthorize("@ss.hasPermi('mesana:defect:remove')")*/
    @Log(title = "疵点管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shaftDefectService.deleteShaftDefectByIds(ids));
    }
}
